package com.rockcent.api.helper;


import com.rockcent.mall.lib.domain.MallWechatConfig;
import com.rockcent.mall.lib.repository.WechatConfigRepository;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.pay.WxPayJsSDKCallback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Marco on 17-3-29.
 */
@Component
public class WechatPaymentHelper {

    private final Logger log = LoggerFactory.getLogger(WechatPaymentHelper.class);

    public static final String sEncodingAESKey = "WtXLiyk28BaGC1ChARcpkgp0FIhqxFTCjNYLm86ucFL"; // 不需要修改
    public static final String sToken = "GWH2015GO"; // 不需要修改

    @Autowired
    private WechatConfigRepository wechatConfigRepository;

    /**
     * 获取微信支付配置
     * @return
     */
    public WxMpService getWxMpService(String domain) {
        MallWechatConfig config = wechatConfigRepository.queryWechatConfigByDomain(domain);
//        log.info("======================>wechat config : {}", config);
        WxMpService wxMpService=null;
        if (config != null) {
            WxMpInMemoryConfigStorage wxMpInMemoryConfigStorage = new WxMpInMemoryConfigStorage();
            wxMpInMemoryConfigStorage.setAppId(config.getAppid()); // 设置微信公众号的appid
            wxMpInMemoryConfigStorage.setSecret(config.getAppSecret()); // 设置微信公众号的app corpSecret
            wxMpInMemoryConfigStorage.setToken(sToken); // 设置微信公众号的token
            wxMpInMemoryConfigStorage.setAesKey(sEncodingAESKey); // 设置微信公众号的EncodingAESKey
            // 微信支付商户号
            wxMpInMemoryConfigStorage.setPartnerId(config.getMchid());
            wxMpInMemoryConfigStorage.setPartnerKey(config.getConfigKey());
            wxMpService = new WxMpServiceImpl();
            wxMpService.setWxMpConfigStorage(wxMpInMemoryConfigStorage);
        }
        return wxMpService;
    }

    /**
     * 转换Map
     * @param payCallback
     * @return
     * @throws IntrospectionException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     */
    public Map<String,String> getPayCallbackDataMap(WxPayJsSDKCallback payCallback)
            throws IntrospectionException,
            IllegalAccessException,
            InvocationTargetException {
        Map<String, String> pmap = new HashMap<String, String>();
        BeanInfo beanInfo = Introspector.getBeanInfo(payCallback.getClass());
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor property : propertyDescriptors) {
            String key = property.getName();
            // 过滤class属性
            if (!key.equals("class") && !key.equals("sign")) {
                // 得到property对应的getter方法
                Method getter = property.getReadMethod();
                Object value = getter.invoke(payCallback);
                if (value != null) {
                    pmap.put(key, value.toString());
                }
            }
        }
        return pmap;
    }
}
